﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.Entity;
using BotonJXC.BLL;
using ComponentFactory.Krypton.Toolkit;

namespace BotonJXC.Forms
{
    public partial class AllocationOrderSearchForm : BaseMdiForm
    {
        #region member
        StoreBLL _storeBLL = new StoreBLL();
        DepartmentBLL _departmentBLL = new DepartmentBLL();
        WarehouseBLL _warehouseBll = new WarehouseBLL();
        IDictionary<int, IList<Warehouse>> _warehouseDict = new Dictionary<int, IList<Warehouse>>();
        #endregion

        #region form load
        public AllocationOrderSearchForm()
        {
            InitializeComponent();
        }       

        private void AllocationOrderSearchForm_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                InitForm();
            }
        }
        void InitForm()
        {
            if (AllocateType == 1)
            {
                this.Text = "分店店内调拨";
                LoadStores(true);
            }
            else if (AllocateType == 2)
            {
                this.Text = "分店同城调拨";
                LoadStores(false);
            }
            else if (AllocateType == 3)
            {
                this.Text = "配送中心店内调拨单";
                LoadStores(true);
            }
            else if (AllocateType == 4)
            {
                this.Text = "加工间店内调拨";
                LoadStores(true);
            }
            else if (AllocateType == 5)
            {
                this.Text = "配送中心行政领用";
                LoadStores(true);
            }
        }
        void LoadStores(bool isSameStore)
        {
            if (isSameStore)
            {
                IList<Store> _stores = _storeBLL.FindAll();
                kcbOutStore.DataSource = kcbInStore.DataSource = _stores;
            }
            else
            {
                IList<Store> _stores = _storeBLL.FindAll();
                kcbOutStore.DataSource = new BindingList<Store>(_stores);
                kcbInStore.DataSource = new BindingList<Store>(_stores);
            }
        }
        #endregion

        # region Property
        public int AllocateType { get; set; }
        public Allocates Allocate { get; set; }
        #endregion

        #region method
        void FillWarehouses(KryptonComboBox storeComboBox, KryptonComboBox warehouseComboBox)
        {
            if (storeComboBox.SelectedIndex > -1)
            {
                IList<Store> stores = (IList<Store>)storeComboBox.DataSource;
                storeComboBox.Tag = stores[storeComboBox.SelectedIndex];
                warehouseComboBox.DataSource = new BindingList<Warehouse>(LoadWarehouses(stores[storeComboBox.SelectedIndex].StoreId));
            }
            else
            {
                warehouseComboBox.DataSource = null;
                storeComboBox.Tag = null;
            }
        }
        IList<Warehouse> LoadWarehouses(int storeId)
        {
            IList<Warehouse> deps = null;
            if (!_warehouseDict.TryGetValue(storeId, out deps))
            {
                deps = _warehouseBll.FindWarehouseByStoreId(storeId, true);
                _warehouseDict.Add(storeId, deps);
            }
            return deps;
        }
        private void BindWarehouse(KryptonComboBox kcbWarehoue)
        {
            if (kcbWarehoue.SelectedIndex > -1)
            {
                kcbWarehoue.Tag = (kcbWarehoue.DataSource as IList<Warehouse>)[kcbWarehoue.SelectedIndex];
            }
            else
            {
                kcbWarehoue.Tag = null;
            }
        }

        /// <summary>
        /// 获取当前单据产生地
        /// </summary>
        /// <returns></returns>
        short GetCreatePlace()
        {
            if (AllocateType == 1 || AllocateType == 2)
            {
                return 1;
            }
            else if (AllocateType == 4)
            {
                return 4;
            }
            else
            {
                return 2;
            }
        }
        #endregion

        #region 控件事件
        private void kbCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void kbOK_Click(object sender, EventArgs e)
        {
            this.Allocate = new Allocates();
            this.Allocate.AllocateSn = kTbxSN.Text;
            this.Allocate = new Allocates();
            this.Allocate.AllocateSn = kTbxSN.Text;
            if (kcbOutWarehouse.Tag != null)
                this.Allocate.OutWarehouse = kcbOutWarehouse.Tag as Warehouse;
            if (kcbInWarehouse.Tag != null)
                this.Allocate.InWarehouse = kcbInWarehouse.Tag as Warehouse;
            this.Allocate.AllocateType = this.AllocateType;
            this.Allocate.CreatePlace = GetCreatePlace();
            this.Allocate.CreateTime = kdtpCreateTime1.Value;
            this.Allocate.EndCreateTime = kdtpCreateTime2.Value;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
        private void kcbOutStore_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillWarehouses(kcbOutStore, kcbOutWarehouse);
        }

        private void kcbInStore_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillWarehouses(kcbInStore, kcbInWarehouse);
        }

        private void kcbOutWarehoue_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindWarehouse(kcbOutWarehouse);
        }

        private void kcbInWarehouse_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindWarehouse(kcbInWarehouse);
        }
        #endregion

    }
}
